package top.iridescent.hashmap;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class HashMapStudy {
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    static final int MAXIMUM_CAPACITY = 1 << 30;

    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    static final int TREEIFY_THRESHOLD = 8;

    static final int UNTREEIFY_THRESHOLD = 6;

    static final int MIN_TREEIFY_CAPACITY = 64;
    Map map = new java.util.HashMap();


    public static void main(String[] args) {
        System.out.println(DEFAULT_INITIAL_CAPACITY);
        System.out.println(MAXIMUM_CAPACITY);
        System.out.println(DEFAULT_LOAD_FACTOR);
        System.out.println(TREEIFY_THRESHOLD);
        System.out.println(UNTREEIFY_THRESHOLD);
        System.out.println(MIN_TREEIFY_CAPACITY);
    }

    public static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

    @Test
    public void test1() {
        System.out.println(tableSizeFor(3));


        HashMap hashMap = new HashMap();
        System.out.println(hashMap);
        Class<?>[] classes = HashMap.class.getInterfaces();
        System.out.println(classes.length);
        for (int i = 0; i < classes.length; i++) {
            System.out.println(classes[i]);
        }

    }

    @Test
    public void test2() {
        HashMap<String, String> map = new HashMap();
        map.put("a1", "1111");
        map.put("a2", "1111");
        map.put("a3", "1111");
        map.put("a4", "1111");
        map.put("a5", "1111");
        map.put("a6", "1111");
        map.put("a7", "1111");
        map.put("a8", "1111");
        map.put("a9", "1111");
        map.put("a10", "1111");
        map.put("a11", "1111");
        map.put("a12", "1111");
        map.put("a13", "1111");
        map.put("a14", "1111");
        map.put("a15", "1111");
        map.put("a16", "1111");
        map.put("a17", "1111");
        map.put("a18", "1111");
        System.out.println(map);
    }

    static final int hash(Object key) {


        int h = key.hashCode();
        int h2 = h >>> 16;
        return h ^ h2;
    }

    @Test
    public void testtt() {
        //System.out.println(hash("222"));
        String x0 = "通话";
        String x1 = "重地";
        String x2 = "方面";
        String x3 = "树人";
        String x4 = "俗话说";
        String x5 = "宁可";
        System.out.println(x0.hashCode());
        System.out.println(x1.hashCode());
        System.out.println(x2.hashCode());
        System.out.println(x3.hashCode());
        System.out.println(x4.hashCode());
        System.out.println(x5.hashCode());
    }

    @Test
    public void testxd() {
        System.out.println('g' + 12);
        System.out.println(Integer.toHexString('g'));
        System.out.println((int) '£');
        System.out.println((char) 1427060);
        char[] val = new char[2];
        val[0] = 'd';
        val[1] = 'd';
        System.out.println(testx5(val));
        System.out.println(testx5(val));

    }

    public static int testx5(char[] val) {
        int h = 0;
        if (h == 0 && val.length > 0) {
            for (int i = 0; i < val.length; i++) {
                h = 31 * h + val[i];
            }
        }
        return h;
    }

    @Test
    public void test3() {
        int n = 2;

        System.out.println(n >>> 1);
        System.out.println(1 | 2);
        n |= n >>> 1;
        System.out.println(n);
        n |= n >>> 2;
        System.out.println(n);
        n |= n >>> 4;
        System.out.println(n);
        n |= n >>> 8;
        System.out.println(n);
        n |= n >>> 16;
        System.out.println(n);


        System.out.println(Integer.MAX_VALUE);
    }

    @Test
    public void test7() {
        HashMap mapx = new HashMap();
        int count = 1;
        while (true) {
            HashMap map = new HashMap();
            mapx.put("xx" + count, map);
            count += 1;
            System.out.println(count);
        }


    }
}
